<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-09
 * Time: 16:02
 */

namespace app\api\service\room;


use app\api\model\Admin;
use app\api\model\HotelRoom;
use app\api\model\HotelRoomBasis;
use app\api\model\HotelRoomClaim;
use app\api\model\HotelRoomFacility;
use app\api\model\HotelRoomImage;
use app\api\model\HotelRoomTag;
use LinCmsTp5\exception\ParameterException;
use think\Db;
use think\Exception;

class UpdateRoom
{
    // post的信息
    protected $putArr;

    function __construct($post)
    {
        $this->putArr = $post;
    }

    // 入口文件
    public function update()
    {
        $info = HotelRoom::get($this->putArr['id']);
        if(!$info) {
            throw new ParameterException([
                'msg' => [
                    'room' => '请重新选择'
                ]
            ]);
        }
        //事务开始
        Db::startTrans();
        try{
            // 删除操作
            HotelRoomImage::destroy(['hotel_room_id'=>$this->putArr['id']]);
            HotelRoomBasis::destroy(['hotel_room_id'=>$this->putArr['id']]);
            HotelRoomTag::destroy(['hotel_room_id'=>$this->putArr['id']]);
            HotelRoomFacility::destroy(['hotel_room_id'=>$this->putArr['id']]);
            HotelRoomClaim::destroy(['hotel_room_id'=>$this->putArr['id']]);
            // 创建房间信息
            $mode = new HotelRoom();
            $mode->allowField(true)->save($this->putArr,['id',$this->putArr['id']]);
            // 图片
            foreach ($this->putArr['imgs'] as $k=>$v) {
                $this->putArr['imgs'][$k]['hotel_room_id'] = $this->putArr['id'];
            }
            $HotelRoomImage = new HotelRoomImage();
            $HotelRoomImage->allowField(true)->saveAll($this->putArr['imgs']);
            // 基础信息
            if(count($this->putArr['basis']) > 0) {
                foreach ($this->putArr['basis'] as $k=>$v) {
                    unset($this->putArr['basis'][$k]['id']);
                    unset($this->putArr['basis'][$k]['create_time']);
                    unset($this->putArr['basis'][$k]['update_time']);
                    $this->putArr['basis'][$k]['hotel_room_id'] = $this->putArr['id'];
                }
                $HotelRoomBasis = new HotelRoomBasis();
                $HotelRoomBasis->allowField(true)->saveAll($this->putArr['basis']);
            }
            // 标签
            if(count($this->putArr['tag']) > 0) {
                foreach ($this->putArr['tag'] as $k=>$v) {
                    unset($this->putArr['tag'][$k]['id']);
                    unset($this->putArr['tag'][$k]['create_time']);
                    unset($this->putArr['tag'][$k]['update_time']);
                    $this->putArr['tag'][$k]['hotel_room_id'] = $this->putArr['id'];
                }
                $HotelRoomTag = new HotelRoomTag();
                $HotelRoomTag->allowField(true)->saveAll($this->putArr['tag']);
            }
            // 设备与服务
            if(count($this->putArr['facility']) > 0) {
                foreach ($this->putArr['facility'] as $k=>$v) {
                    unset($this->putArr['facility'][$k]['id']);
                    unset($this->putArr['facility'][$k]['create_time']);
                    unset($this->putArr['facility'][$k]['update_time']);
                    $this->putArr['facility'][$k]['hotel_room_id'] = $this->putArr['id'];
                }
                $HotelRoomFacility = new HotelRoomFacility();
                $HotelRoomFacility->allowField(true)->saveAll($this->putArr['facility']);
            }
            // 入住要求
            if(count($this->putArr['claim']) > 0) {
                foreach ($this->putArr['claim'] as $k=>$v) {
                    unset($this->putArr['claim'][$k]['id']);
                    unset($this->putArr['claim'][$k]['create_time']);
                    unset($this->putArr['claim'][$k]['update_time']);
                    $this->putArr['claim'][$k]['hotel_room_id'] = $this->putArr['id'];
                }
                $HotelRoomClaim = new HotelRoomClaim();
                $HotelRoomClaim->allowField(true)->saveAll($this->putArr['claim']);
            }
            //事务结束  开始到结束中间出现错误以后,先执行的添加就不会生成记录
            Db::commit();
            return writeJson(202, [], '修改成功！');
        }
        catch (Exception $ex){
            //回滚事务
            Db::rollback();
            throw $ex;
        }
    }
}